//
//  ZFBHomeFunctionListController.m
//  支付宝
//
//  Created by HaoYoson on 16/6/28.
//  Copyright © 2016年 HaoYoson. All rights reserved.
//

#import "NSArray+Addition.h"
#import "UIColor+Addition.h"
#import "ZFBCycleCell.h"
#import "ZFBFunctionListCell.h"
#import "ZFBFunctionListLayout.h"
#import "ZFBHomeFunctionListController.h"

#define kFirstSectionCount 12

static NSString *cellid = @"functionList_cell";
static NSString *adcellid = @"ad_cell";

@interface ZFBFunctionListController () <UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *functionListData;

@end

@implementation ZFBFunctionListController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 第0组和第2组的cell
    [self.collectionView registerClass:[ZFBFunctionListCell class] forCellWithReuseIdentifier:cellid];
    // 第1组cell
    [self.collectionView registerClass:[ZFBCycleCell class] forCellWithReuseIdentifier:adcellid];

    self.functionListData = [self loadFunctionListData];

    // 底边多滑49
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);

    // 修改背景颜色
    self.collectionView.backgroundColor = [UIColor colorWithHex:0xf5f5f9];
}

- (NSArray *)loadFunctionListData {
    return [NSArray objectListWithPlistName:@"FunctionsList.plist" clsName:@"ZFBFunction"];
}

/**
 * 分组数量
 * 第 0 组：前12个
 * 第 1 组：广告位
 * 第 2 组：剩余部分
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return kFirstSectionCount;
    } else if (section == 1) {
        return 1;
    } else if (section == 2) {
        return self.functionListData.count - kFirstSectionCount;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 如果是第一组 反悔红色广告cell
    if (indexPath.section == 1) {
        ZFBCycleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:adcellid forIndexPath:indexPath];
        cell.imageURLs = [self imageURLs];
        return cell;
    }

    // 如果是第零组和第二组 返回functionListCell
    ZFBFunctionListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];

    switch (indexPath.section) {
        case 0:
            cell.function = self.functionListData[indexPath.row];
            break;
        case 2:
            cell.function = self.functionListData[indexPath.row + kFirstSectionCount];
            break;

        default:
            break;
    }

    return cell;
}

// 每个itemsize的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return CGSizeMake(self.collectionView.bounds.size.width, 100);
    }
    // 每行显示的格子数量
    NSInteger count = 4;
    CGFloat wh = (self.collectionView.bounds.size.width - (count - 1)) / count;
    return CGSizeMake(wh, wh);
}

- (NSArray *)imageURLs {
    NSMutableArray *array = [NSMutableArray array];

    for (int i = 0; i < 5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"Home_Scroll_%02zd.jpg", i + 1];

        [array addObject:[[NSBundle mainBundle] URLForResource:imageName withExtension:nil]];
    }

    [array addObject:[NSURL URLWithString:@"http://pic18.nipic.com/20111220/8478161_214305641176_2.jpg"]];

    return array.copy;
}

@end